package com.mongodb.mongodbboot.Future;

import cn.hutool.core.collection.CollectionUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;

/**
 * @Author huyunqiang
 * @Description
 * @Date 2025/6/19 19:36
 **/
public class FutureEquip {

    public static void main(String[] args) {
        try {
            jsEquip();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void jsEquip() throws InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        //多线程返回值map
        Map<Integer, Future<Boolean>> futureMap = new HashMap<>();

        List<String> mpIds= Arrays.asList("1","2","3","4","5","6","7");

        long st = System.currentTimeMillis();
        for(int i =0;i<mpIds.size();i++){
            int finalI = i;
            Callable<Boolean> workCall = new Callable<Boolean>() {
                @Override
                public Boolean call() throws InterruptedException {
                    //执行的任务
                    taskEquip(mpIds.get(finalI));
                    return true;
                }
            };
            //提交callable到线程池
            futureMap.put(i, executorService.submit(workCall));

        }
        executorService.shutdown();
        System.out.println("haoshi：="+(System.currentTimeMillis() -st));

        if (CollectionUtil.isNotEmpty(futureMap)) {
            for (Integer k : futureMap.keySet()) {
                //返回计算结果是否异常
                Boolean result = null;
                try {
                    result = futureMap.get(k).get();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
                System.out.println("yeshu=:" + k + "|status=:" + result);
            }
        }

    }


    public static void taskEquip(String s) throws InterruptedException {
        TimeUnit.SECONDS.sleep(1);
        System.out.println("---s==="+s);
    }
}
